<!-- 快照回滚 -->
<template>
  <div>
    <a-modal 
      v-model="modalVisible" 
      @ok='handleRollBackSnap'
      @cancel="handleCancel"
      :maskClosable="false"
      :width="400"
      v-dragModal
    >
      <template slot="title">
        <!-- <i class="icon icon-exclaim64f tip-icon"></i> -->
        <Iconfont class="icon tip-icon" type="icon-exclaim64f"></Iconfont>
        <span v-if="$store.state.language=='zh'">快照回滚</span>
        <span v-else>Snapshot Rollback</span>
      </template>

      <div>
        <span>
          {{$t('Sure_rollback') + targetSnapshot.snap+' ?'}}
        </span>
      </div>

      <template slot="footer">
        <a-button @click="handleCancel" class="submit-btn">
          {{$t('Cancel')}}
        </a-button>
        <a-button class="submit-btn" type="primary" @click="handleRollBackSnap" :loading="submitLoading" >
          {{$t('Sure')}}
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
export default {
  name: "RollBackSnap",
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    targetSnapshot: {
      type: Object,
      required: true
    },
    targetBlock: {
      type: Object,
      required: true,
      default: () => { return {} }
    }
  },

  data() {
    return {
      modalVisible: false,
      submitLoading: false,
    }
  },

  watch: {
    visible(newValue) {
      this.modalVisible = newValue;
    }
  },

  methods: {
    // 回滚快照
    async handleRollBackSnap() {
      this.submitLoading = true;
      const params = {
        pool_id: this.targetBlock.pool_id,
        image_id: this.targetBlock.id,
        snapshot_id: this.targetSnapshot.snap_id,
        snapshot_name: this.targetSnapshot.snap,
        pool_name: this.targetBlock.pool_name,
        block_name: this.targetBlock.name,
      };
      try {
        const res = await this.$api.blockInfo.rollBackSnapRequest(params);
        if(res && res.data) {
          this.model(res.data.id) //返回模板
          this.notice(res.data.id,res.data.params,res.data.flag) //通知列表显示
          this.$emit('updateInfo');
        }
      }catch {
        this.handleCancel();
      }
      this.handleCancel();
    },

    //关闭弹窗
    handleCancel() {
      this.$emit('closeOperationModal', 'roll-back');
      this.submitLoading = false;
    },
  },
}
</script>
<style scoped>
  .tip-icon {
    color: var(--mainOrangeColor);
    margin-right: 8px;
  }
  .submit-btn {
    width: 70px;
  }
  ::v-deep.ant-modal-header {
    border-bottom: none;
  }
  ::v-deep.ant-modal-footer {
    border-top: none;
  }
  ::v-deep.ant-modal-body {
    padding: 12px 24px;
  }
</style>